package lt.inkredibl.iit;

public class Matrix<T>
{
	private int _width;
	private int _height;
	private T[][] _matrix;

	public Matrix(int width, int height)
	{
		_width = width;
		_height = height;
		_matrix = makeArrayOfTArrays(_width);
		for (int i = 0; i < _width; i++)
		{
			_matrix[i] = makeArrayOfT(_height);
		}
	}

	@SuppressWarnings("unchecked")
    private T[] makeArrayOfT(int size)
    {
	    return (T[])new Object[size];
    }

	@SuppressWarnings("unchecked")
    private T[][] makeArrayOfTArrays(int size)
    {
	    return (T[][])new Object[size][];
    }
	
	public T get(int x, int y)
	{
		return _matrix[x][y];
	}
	
	public void set(int x, int y, T obj)
	{
		_matrix[x][y] = obj;
	}
	
	public void setCol(int x, Iterable<? extends T> col)
	{
		int i = 0;
		for (T o: col)
		{
			_matrix[x][i++] = o;
		}
	}
	
	public void setRow(int y, Iterable<? extends T> row)
	{
		int i = 0;
		for (T o: row)
		{
			_matrix[i++][y] = o;
		}
	}
	
	public int getWidth()
	{
		return _width;
	}
	
	public int getHeight()
	{
		return _height;
	}
}
